'use strict';
/****************************************** 
 * 通用函数库
 ******************************************/
/**
 * Form表单数据formData转化为Json
 * @returns {$.fn.serializeFormObject@pro;value|RegExp.fn.serializeFormObject.o|String|Array}
 */
$.fn.formToJson = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
var sys = {
    /**
     * Form表单数据formData转化为Json
     * 
     * @param {type} form
     * @returns {jQuery}
     */
    formToJson: function (form) {
        var data = {};
        $('input[type=hidden],input[type=text],input[type=password],select', form).each(function () {
            data[this.name] = $(this).val();
        });
        return data;
    },
    /**
     * 显示周期Duration
     * @param {type} duration
     * @returns {String}
     */
    printDuration: function (duration) {
        var months = parseInt(duration.years) * 12 + parseInt(duration.months), days = parseInt(duration.days), result = '';
        if (months !== 0)
            result += months + '个月';
        if (days !== 0)
            result += days + '天';
        return result === '' ? '未知' : result;
    },
    /**
     * 获取借款状态key值
     * @param {type} status_name
     * @returns {sysLoanStatus}
     */
    getLoanStatus: function (status_name) {
        return sysLoanStatus[status_name];
    },
    /**
     * 获取借款状态说明
     * @param {type} status_name
     * @returns {sysLoanStatus}
     */
    getLoanStatusDesc: function (status_name) {
        return sysLoanStatusDesc[status_name];
    },
    /**
     * 获取还款方式key值
     * @param {type} status_name
     * @returns {sysLoanStatus}
     */
    getRepayMethod: function (method_name) {
        return sysRepayMethod[method_name];
    },
    /**
     * 封装Mustache渲染模板
     * @param {type} selector
     * @param {type} data
     * @returns {unresolved}
     */
    render: function (selector, data) {
        var template = $(selector).html();
        Mustache.parse(template);
        return Mustache.render(template, data);
    },
    isInt: function (n) {
        return !isNaN(parseInt(n)) && /^\d*$/.test(n);
    },
    loading: function (el) {
        el.html('<i class="fa fa-spin fa-spinner mr10"></i>加载中 ...');
    },
    btnSpin: function (btn) {
        btn.data("default", btn.html());
        btn.html('<i class="fa fa-spinner fa-spin"></i> 处理中').prop("disabled", true);
    },
    btnStop: function (btn) {
        btn.html(btn.data("default")).prop("disabled", false);
    },
    confirm: function (msg, confirmFunc) {
        $("#modalConfirmBtn").html('<i class="fa fa-check-circle mr10"></i>确定').prop("disabled", false);
        $.magnificPopup.open({
            removalDelay: 500, //delay removal by X to allow out-animation,
            alignTop: true,
            closeOnBgClick: false,
            items: {
                src: "#modalConfirm"
            },
            // overflowY: 'hidden', // 
            callbacks: {
                beforeOpen: function (e) {
                    this.st.mainClass = 'mfp-slideDown';
                    $("#modalConfirmText").html(msg);
                    if (confirmFunc) {
                        $("#modalConfirmBtn").unbind('click').on("click", function (e) {
                            confirmFunc($(this));
                        });
                    }
                }
            },
            midClick: true // allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source.
        });
    },
    alert: function (msg, confirmFunc) {
        $.magnificPopup.open({
            removalDelay: 500, //delay removal by X to allow out-animation,
            alignTop: true,
            closeOnBgClick: false,
            items: {
                src: "#modalAlert"
            },
            callbacks: {
                beforeOpen: function (e) {
                    this.st.mainClass = 'mfp-slideDown';
                    $("#modalAlertText").html(msg);
                    if (confirmFunc) {
                        $("#modalAlertBtn").unbind('click').on("click", function () {
                            confirmFunc($(this));
                        });
                    }
                }
            },
            midClick: true // allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source.
        });
    },
    closeModal: function () {
        $.magnificPopup.close();
    },
    processModal: function (msg) {
        $.magnificPopup.open({
            removalDelay: 500, //delay removal by X to allow out-animation,
            alignTop: true,
            closeOnBgClick: false,
            items: {
                src: "#modalProcess"
            },
            callbacks: {
                beforeOpen: function (e) {
                    this.st.mainClass = 'mfp-slideDown';
                    if (msg) {
                        $("#modalProcessText").html(msg);
                    }
                }
            },
            midClick: true // allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source.
        });
    },
    /**
     * 检查当前浏览器是否为Chrome
     */
    browserSupport: function () {
//        $.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
//        $.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
//        $.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
//        $.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());
        return /webkit/.test(navigator.userAgent.toLowerCase());
    }
};

/****************************************** 
 * 系统常量
 ******************************************/
/**
 * 年化利率最大值
 */
var sysMaxLoanRate = 5000;
/**
 * 年化利率最小值
 */
var sysMinLoanRate = 0;
/**
 * 借款状态
 */
var sysLoanStatus = {
    PROPOSED: "待审核", //("未处理"),
    REJECTED: "未通过", //("未通过"),
    APPROVED: "已批准", //("已批准"),
    SCHEDULED: "已排期", //("已安排"),
    OPEN: "筹款中", //("开放投标"),
    FAILED: "已流标", //("流标"),
    FINISHED: "已满标", //("已满标"),
    LOANED: "还款中", //("已放款"),
    CLEARED: "已还清", //("已还清"),
    OVERDUE: "已逾期", //("逾期"),
    CANCELED: "已取消", //("已取消"),
    ARCHIVED: "已存档" //("已存档")
};
/**
 * 可编辑标的状态
 */
var sysEditableStatus = [
    'PROPOSED',
    'REJECTED',
    'APPROVED'
];
/**
 * 可部分编辑的状态
 */
var sysPartlyEditableStatus = [
    'SCHEDULED',
    'OPEN',
    'CANCELED', // 避免发标错误导致标的唯一号/标的标题被占，无法修改标的唯一号/标的标题
    'ARCHIVED'
];
/**
 * 全部不可编辑的状态
 * FAILED,
 * FINISHED,
 * LOANED,
 * CLEARED,
 * OVERDUE
 */

var sysLoanStatusDesc = {
    PROPOSED: "等待审核", //("未处理"),
    REJECTED: "审核不通过", //("审核不通过"),
    APPROVED: "已通过审核，等待排期", //("已批准"),
    SCHEDULED: "已排期，即将开标", //("已安排"),
    OPEN: "开放投标中", //("开放投标"),
    FAILED: "已流标", //("流标"),
    FINISHED: "已满标", //("已满标"),
    CANCELED: "已取消", //("已取消"),
    LOANED: "还款中", //("已放款"),
    CLEARED: "已还清借款", //("已还清"),
    OVERDUE: "还款逾期", //("逾期");
    ARCHIVED: "存档，前台不显示"//("存档")
};
/**
 * 标的类型
 */
var sysLoanProduct = {
    NEWBEE: "新手标",    
    GUARANTEE: "担保标",
    BANK:"银行汇票",
    BUSINESS:"商业汇票",
    SECKILL:"秒杀标"
};
/**
 * 还款方式
 */
var sysRepayMethod = {
    INTEREST: "按月付息到期还本",
    EQUAL_INSTALLMENT: "等额本息",
    EQUAL_PRINCIPAL: "等额本金",
    BULLET_REPAYMENT: "一次性还本付息"
};
/**
 * 投标状态
 */
var sysInvestStatus = {
    PROPOSED: "已申请",
    TIMEOUT: "已超时",
    FROZEN: "已冻结",
    FROZEN_FAILED: "资金冻结失败",
    CANCELED: "已取消",
    LOANED: "已放款",
    CLEARED: "已还清",
    OVERDUE: "已逾期"
};
/**
 * 资金操作类型
 */
var sysFundOperation = {
    FREEZE: "冻结",
    UNFREEZE: "解冻",
    IN: "转入",
    OUT: "转出"
};
/**
 * 资金记录状态
 */
var sysFundStatus = {
    I: "初始",
    P: "处理中",
    A: "审核中",
    S: "成功",
    F: "失败",
    R: "拒绝",
    C: "取消",
    O: "在途"
};
/**
 * 资金类型
 */
var sysFundType = {
    INVEST: "投标",
    WITHDRAW: "取现",
    DEPOSIT: "充值",
    LOAN: "放款",
    REPAY: "还款",
    REIMBURSE: "垫付",
    CREDIT_ASSIGN: "债权转让",
    TRANSFER: "转账",
    PENALTY: "逾期罚息",
    /**
     * fee related
     */
    FEE_WITHDRAW: "提现手续费",
    FEE_DEPOSIT: "充值手续费",
    FEE_REPAY_INVEST: "还款管理费(收取投资人)",
    FEE_REPAY_LOAN: "还款管理费(收取借款人)",
    FEE_LOAN_SERVICE: "借款服务费",
    FEE_LOAN_MANAGE: "借款管理费",
    FEE_LOAN_GUARANTEE: "借款担保费",
    FEE_OVERDUE_PLAT: "逾期管理费(给商户)",
    FEE_OVERDUE_INVEST: "逾期管理费(给投资人)",
    FEE_UNDUE_PLAT: "提前还款违约金(给商户)",
    FEE_UNDUE_INVEST: "提前还款违约金(给投资人)",
    FEE_CREDIT_ASSIGN: "债权转让手续费",
    UPDATE_COMMODITY_OPERATION:"更改商品操作日志"
};
/**
 * 还款状态
 */
var sysRepayStatus = {
    UNDUE: "未到期",
    OVERDUE: "逾期",
    REPAYED: "已还清"
};
/**
 * 红包类型
 */
var sysCouponType = {
    CASH: "现金",
    REBATE: "返利"
};
/**
 * 红包奖励事件类型
 */
var sysRewardEventType = {
    USER_REGISTER: "用户注册",    
    USER_REFEREE: "推荐好友",
    USER_INVEST_POINT: "用户投资送积分",
    USER_INVEST_CYCLE: "老带新",    
    USER_REGISTER_COUPON: "用户注册给积分",
    POINT_EXCHANGE_COUPON: "积分换红包",
    USER_RECOMMEND_CAPITAL:"投资推荐红包"
};
/**
 * 红包状态
 */
var sysRewardStatus = {
    CREATED: "已创建",
    ALLOCATED: "已分配",
    INVOKED: "已使用",
    QUALIFIED: "已达标",
    PAYED: "已打款",
    EXPIRED: "已过期",
    CANCELED: "已取消",
    ARCHIVED: "已存档"
};

/**
 * 积分
 */
var sysPointsOperation = {
    
    IN:"获得",
    OUT:"消费",
    FROZEN:"冻结",
    THAW:"解冻",
    SIGN_IN:"签到",
    OPEN_ACCOUNT:"开户",
    FIRST_RECHARGE:"首次充值",
    FIRST_BIND_CARD:"首次绑卡",    
    RETURN: "退回"
};
/**
 * 积分类型
 */
var sysPointType={
    //纯粹行为部分(收入ACTION)
    SIGN_IN:"签到",
    OPEN_ACCOUNT:"开户",
    FIRST_RECHARGE:"首次充值",
    FIRST_BIND_CARD:"首次绑卡",
    USER_REGISTER:"用户注册",
    USER_REGISTER_COUPON:"积分红包",
    ACTION:"解冻",
	
    //投资收入部份(INVEST)
    USER_INVEST:"投资",
	
    //竞价支出部分(AUCTION)
    AUCTION:"竞价",
    
    //兑换支出部分(EXCHANGE)
    USER_EXCHANGE_TICKET:"兑换红包",    
    USER_EXCHANGE_COMMODITY:"兑换商品",
    USER_EXCHANGE_RETURN:"退订商品",
    //手动扣除积分
    MANUAL_DEDUCTION_POINTS:"手动扣除",
    MANUAL_INCREASE_POINTS:"手动增加",	
    //抽奖与积分夺宝
    USER_PURE_LOTTERY:"抽奖",
    USER_CROWD_FUND:"积分夺宝",
    RECOMMEND_INVEST:"推荐投资"
};

/**
 * 回访用户类别
 */
var sysUserType = {
    VIP: "VIP",
    SENIOR: "高级",
    ORDINARY: "普通",
    A: "A",
    B: "B",
    C: "C"
};
/**
 * 积分管理商品类型
 */
var sysCommodityType = {
    COMMODITY:"商品",
    REDPACKET:"红包",
    NONE:"零值产品",
    COUPON:"奖券"
};
